package edu.gatech.cs2340.aperturescience.map;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

import edu.gatech.cs2340.aperturescience.R;

/**
 * Google Maps class to indicate location of tasks.
 * 
 * @author Kefu Zhou
 */

public class MyGoogleMap extends MapActivity {

	@Override
	/**
	 * The route will not be displayed.
	 * @return false
	 */
	protected boolean isRouteDisplayed() {
		return false;
	}

	// GeoPoint geoPoint = new GeoPoint((int) (52.334822 * 1E6),
	// (int) (4.668907 * 1E6));
	private MapView mapView;
	private MapController mc;

	@Override
	/**
	 * Sets up the map.
	 * @param savedInstanceState
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.googlemap);

		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		mapView.setSatellite(true);

		mc = mapView.getController();
		mc.setZoom(18);

		List<Overlay> overlays = mapView.getOverlays();
		overlays.clear();
//		geoPoint = 
//		overlays.add(new MyOverlay(geoPoint));
//
//		mapView.invalidate();


	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		Projection p = mapView.getProjection();
		GeoPoint geoPoint = p
				.fromPixels((int) event.getX(), (int) event.getY());
		List<Overlay> overlays = mapView.getOverlays();
		overlays.clear();
		Log.d("drawmarker", "draw a marker");
		overlays.add(new MyOverlay(geoPoint));

		mapView.invalidate();
		return true;

	}

	private class MyOverlay extends com.google.android.maps.Overlay {
		GeoPoint gp;

		public MyOverlay(GeoPoint gp) {
			this.gp = gp;
			mc.animateTo(gp);

		}

		@Override
		public boolean onTouchEvent(MotionEvent event, MapView mapView) {
			Projection p = mapView.getProjection();
			GeoPoint geoPoint = p.fromPixels((int) event.getX(),
					(int) event.getY());
			List<Overlay> overlays = mapView.getOverlays();
			overlays.clear();
			Log.d("drawmarker", "draw a marker");
			overlays.add(new MyOverlay(geoPoint));

			mapView.invalidate();
			return true;
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) { // 1
			super.draw(canvas, mapView, shadow);

			if (!shadow) { // 2

				Point point = new Point();
				mapView.getProjection().toPixels(gp, point); // 3

				Bitmap bmp = BitmapFactory.decodeResource(getResources(),
						R.drawable.pushpin); // 4

				int x = point.x - bmp.getWidth() / 2; // 5

				int y = point.y - bmp.getHeight(); // 6

				canvas.drawBitmap(bmp, x, y, null); // 7
			}

		}

	}
}
